package com.sskj.finance.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.bean.BalanceCqtfBean;
import com.sskj.finance.bean.FinanBannerBean;
import com.sskj.finance.bean.FinanBean;
import com.sskj.finance.http.HttpConfig;
import com.sskj.finance.ui.fragment.FinanceFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class FinanceFragmentPresenter extends BasePresenter<FinanceFragment> {


    /**
     * 理财产品列表
     *
     * @return
     */
    public Flowable<List<FinanBean>> finanList(String p, String size) {
        return httpService.finanList(p, size)
                .converter(new JsonConverter<HttpData<PageBean<FinanBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());
    }

    /**
     * 轮播通知
     *
     * @return
     */

    public void finanBanner() {
        httpService.finanBanner().execute(new JsonCallBack<HttpData<List<String>>>() {
            @Override
            public void onSuccess(Response<HttpData<List<String>>> response) {
                HttpData<List<String>> httpData = response.body();

                if (httpData.getStatus() == HttpConfig.OK) {
                    mView.updateBanner(httpData.getData());
                }
            }

            @Override
            public void onError(Response<HttpData<List<String>>> response) {
                super.onError(response);
            }
        });
    }

    public void getBalance() {
        httpService.getBalance()
                .execute(new JsonCallBack<HttpData<BalanceCqtfBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<BalanceCqtfBean>> response) {
                        HttpData<BalanceCqtfBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateBalance(httpData.getData());
                        }
                    }
                });
    }


}
